<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI 文本优化</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        textarea {
            width: 100%;
            height: 200px;
            margin: 10px 0;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            resize: vertical;
        }
        select {
            width: 100%;
            padding: 8px;
            margin: 10px 0;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            opacity: 0.9;
        }
        #result {
            margin-top: 20px;
            white-space: pre-wrap;
        }
        .error {
            color: red;
            margin: 10px 0;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>AI 文本优化</h1>
        <div>
            <textarea id="inputText" placeholder="请输入需要优化的文本..."></textarea>
        </div>
        <div>
            <select id="modelSelect">
                <!-- 模型选项将通过 JavaScript 动态加载 -->
            </select>
        </div>
        <div>
            <textarea id="instruction" placeholder="请输入优化指令（可选）...">请帮助优化和完善以下文本，使其更加通顺、准确。</textarea>
        </div>
        <button onclick="optimizeText()">优化文本</button>
        <div id="error" class="error" style="display: none;"></div>
        <div id="result"></div>
    </div>

    <script>
        // 页面加载时获取可用的模型列表
        document.addEventListener('DOMContentLoaded', loadModels);

        async function loadModels() {
            try {
                const response = await fetch('/models');
                if (response.ok) {
                    const data = await response.json();
                    const modelSelect = document.getElementById('modelSelect');
                    modelSelect.innerHTML = data.models.map(model => 
                        `<option value="${model.id}">${model.name}</option>`
                    ).join('');
                }
            } catch (err) {
                showError('加载模型列表失败: ' + err.message);
            }
        }

        async function optimizeText() {
            const text = document.getElementById('inputText').value.trim();
            const model = document.getElementById('modelSelect').value;
            const instruction = document.getElementById('instruction').value.trim();
            const error = document.getElementById('error');
            const result = document.getElementById('result');

            if (!text) {
                showError('请输入需要优化的文本');
                return;
            }

            try {
                const response = await fetch('/optimize', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        text: text,
                        model: model,
                        instruction: instruction
                    })
                });

                if (response.ok) {
                    const data = await response.json();
                    error.style.display = 'none';
                    result.textContent = data.optimized_text;
                } else {
                    const errorData = await response.json();
                    throw new Error(errorData.detail || errorData.error || '优化失败');
                }
            } catch (err) {
                showError(err.message);
            }
        }

        function showError(message) {
            const error = document.getElementById('error');
            error.textContent = message;
            error.style.display = 'block';
            document.getElementById('result').textContent = '';
        }
    </script>
</body>
</html> 